package com.hyt.it.ogt.kq.main.controller.gov;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.kq.common.config.ConfigManager;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.service.gov.service.IOfficeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 机构 前端控制器
 * </p>
 *
 * @author huangyh
 * @since 2021-04-06
 */
@RestController
@Api(tags = "121.机构接口", value = "区域接口")
@ApiSort(value = 121)
@RequestMapping("/kw/gov/office")
@Slf4j
public class OfficeController extends BaseController {

    @Autowired
    private IOfficeService iOfficeService;
    @Resource
    private ConfigManager configManager;

    @ApiOperation(
            value = "21.1 初始化机构数据",
            notes = "30000:初始化机构数据成功; 30070:初始化机构数据异常; 30103:当前机构已经初始化过数据; 30120:未配置系统级行政区划数据; 30121:未配置系统级考试协议数据; 30122:未配置系统级违纪处理数据; 30123:未配置系统级违纪行为数据"
    )
    @ApiOperationSort(value = 1)
    @RequestMapping(value = "/initOfficeData", method = RequestMethod.POST)
    public ApiResponse<Object> initOfficeData() {
        try {
            String officeId = getOfficeId();
            iOfficeService.initOfficeData(officeId);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException kwGovException) {
            log.error("# 初始化机构数据异常: ", kwGovException.getErrMsg());
            return ApiResponse.builder()
                    .code(kwGovException.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 初始化机构数据异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ORGANIZATION_INIT_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "21.2 获取平台端访问地址",
            notes = "30000:初始化机构数据成功;"
    )
    @ApiOperationSort(value = 2)
    @GetMapping("/getGlUrl")
    public ApiResponse<Object> getGlUrl() {
        try {
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(configManager.getGlURL())
                    .build();
        } catch (Exception e) {
            log.error("# 获取平台端访问地址异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.GL_LOGIN_URL_GET.getCode())
                    .build();
        }
    }

}
